<?php
include 'sysco.function.php';

$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);

// ============================= Top Twenty ======================================//

$order = 'TOTAL';
//$order = 'TOTAL_SALE_COUNT';


// $dataTopTwBySalesCount = topTwenty($MERCHANT, $order);
$dataTopTwBySalesCount = getLessOrders($MERCHANT, 20, $order, 2);

$totAmount = 0; $totCount = 0;
$prodIds = array(); 
foreach ($dataTopTwBySalesCount as $data) {
  $prodIds[] = $data['PRODUCT_ID'];  
}
$products = getProductByIds($MERCHANT, $prodIds);
$allOrderCount = getAllOrderCount($MERCHANT);
$totCount = $allOrderCount['TOTAL_SALE_COUNT'];
$totAmount = $allOrderCount['TOTAL'];

///// prev data /////
if ($month == 1) {
  $_SESSION['from'] = '2012-12-01 03:00:00';
  $_SESSION['to'] = '2013-01-01 02:59:00';
} else if ($month == 2) {
  $_SESSION['from'] = '2013-01-01 03:00:00';
  $_SESSION['to'] = '2013-02-01 02:59:00';
}
// $prevSales = topTwenty($MERCHANT, $order, 10000);
$prevSales = getLessOrders($MERCHANT, 10000, $order, 2);
$totCountPV = 0; $totAmountPV = 0;
$allOrderCountPV = getAllOrderCount($MERCHANT);
$totCountPV = $allOrderCountPV['TOTAL_SALE_COUNT'];
$totAmountPV = $allOrderCountPV['TOTAL'];

$fp = fopen('reports/sysco_top20_'.$_SESSION['from'].'-'.$MERCHANT.'.csv', 'a');
echo 'sysco_top20_'.$_SESSION['from'].'-'.$MERCHANT.'.csv'."\n";

$arr = array('------order BY-----',$order,'-----------------');
fputcsv($fp, $arr);

$arr = array('Rank','Rank Trend','Item','Code','Price','Units','Trend', 'Tot', 'Trend', 'Revenue', 'Tot');
fputcsv($fp, $arr);

foreach ($dataTopTwBySalesCount as $data) {
  $product = $products[$data['PRODUCT_ID']];
  $trendC = 'N/A'; $trendR = ''; $trendPR = ''; 
  if (isset($prevSales[$data['PRODUCT_ID']])) {
    $trendC = $data['TOTAL'] - $prevSales[$data['PRODUCT_ID']]['TOTAL'];
    $trendC = ($trendC == 0) ? '' : (($trendC > 0) ? 'UP' : 'DOWN');
    $trendR = $data['RANK'] - $prevSales[$data['PRODUCT_ID']]['RANK'];
    $trendR = ($trendR == 0) ? '' : (($trendR < 0) ? 'UP' : 'DOWN');
    $trendPR = $data['TOTAL']/$totAmount - $prevSales[$data['PRODUCT_ID']]['TOTAL']/$totAmountPV;
    $trendPR = ($trendPR == 0) ? '' : (($trendPR > 0) ? 'UP' : 'DOWN');
  }
  $arr = array($data['RANK'], $trendR, $product['NAME'], $product['CODE'], $product['PRICESELL'], $data['TOTAL_SALE_COUNT'],$trendC, round(($data['TOTAL_SALE_COUNT']/$totCount)*100).'%', $trendPR, number_format($data['TOTAL'], 2), round(($data['TOTAL']/$totAmount)*100).'%');
  fputcsv($fp, $arr);
}

fclose($fp);



/*

$arr = array('','');
fputcsv($fp, $arr);
$arr = array('Item','Code','Price','Units','% Tot', 'Revenue', 'Tot %');
fputcsv($fp, $arr);
// by revenu
$_SESSION['from'] = '2013-01-01 03:00:00';
$_SESSION['to'] = '2013-02-01 02:59:00';

$order = 'TOTAL';
$dataTopTwByTotal = topTwenty($MERCHANT, $order);
$totAmount = 0; $totCount = 0;
$prodIds = array();
foreach ($dataTopTwByTotal as $data) {
  $totCount+= $data['TOTAL_SALE_COUNT'];
  $totAmount+= $data['TOTAL'];
  $prodIds[] = $data['PRODUCT_ID'];  
}
$products = getItemSalesByIds($MERCHANT, $prodIds);

/////// prev data /////
$_SESSION['from'] = '2012-12-01 03:00:00';
$_SESSION['to'] = '2013-01-01 02:59:00';
$prevSales = getItemSalesByIds($MERCHANT, $prodIds);

foreach ($dataTopTwByTotal as $data) {
  $product = $products[$data['PRODUCT_ID']];
  $arr = array($product['NAME'], $product['CODE'], $product['PRICESELL'], $data['TOTAL_SALE_COUNT'],($data['TOTAL_SALE_COUNT']/$totCount)*100, $data['TOTAL'], ($data['TOTAL']/$totAmount)*100);
  fputcsv($fp, $arr);
}
// ============================= Top Twenty ======================================//

fclose($fp);
*/
?>
